package org.sfx.diagnostics

import org.sfx.rich.Enrich._
import javafx.scene.control.TreeItem
import javafx.scene.Node
import javafx.scene.Parent
import scala.collection.JavaConverters._
import javafx.scene.Scene
import javafx.collections.ObservableList
import javafx.beans.InvalidationListener
import javafx.scene.control.TreeItem.TreeModificationEvent
import javafx.event.EventType

case class SpyTreeItem(val spyInfo: SpyInfo) extends TreeItem[SpyInfo](spyInfo, null) {
  def isRoot = false
  
  override def isLeaf(): Boolean = spyInfo.isLeaf
  
  lazy val populate: ObservableList[TreeItem[SpyInfo]] = {
    spyInfo.children.translateInto(this.getChildren()) { infoChild =>
      new SpyTreeItem(infoChild)
    }
    
    spyInfo.children.addListener(new InvalidationListener() {
      override def invalidated(observable: javafx.beans.Observable): Unit = {
        /*
        javafx.event.Event.fireEvent(
            SpyTreeItem.this,
            new TreeModificationEvent(CHILDREN_MODIFICATION_EVENT, this, paramList1, paramList2))*/
      }
    })
    
    this.getChildren()
  }
  
  if (isRoot) {
    this.populate.asScala.foreach { _.asInstanceOf[SpyTreeItem].populate }
  }

  this.expandedProperty().addFirstChangeListener { (_, _, newExpanded) =>
    this.populate.asScala.foreach { _.asInstanceOf[SpyTreeItem].populate }
  }
}

/*object SpyTreeItem {
  val CHILDREN_MODIFICATION_EVENT = new EventType(treeItemCountChangeEvent(), "ChildrenModificationEvent")
}*/
